from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
import time


class config:
    def __init__(self,driver):
        self.driver=driver

    def get_url(self,url):
        self.driver.get(url)

    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as f:
            print(f)

    def find_elements(self, loc):
        try:
            for i in self.driver.find_elements(*loc):
                return print(i.text)
        except Exception as f:
            print(f)

    def click(self, loc):
        click = WebDriverWait(self.driver, 20, 0.5).until(EC.element_to_be_clickable(self.find_element(loc)))
        click.click()

    def send_keys(self, loc, info):
        self.find_element(loc).send_keys(info)
    #清除
    def clear(self,loc):
        try:
            self.find_element(loc).clear()
        except Exception as e:
            print(f"清除失败，原因：{e}")
    #获取文本
    def text(self,loc):
        try:
           return self.find_element(loc).text
        except Exception as e:
            print(f'获取文本失败，原因：{e}')

    # 获取文本
    def texts(self, loc):
        try:
            s=self.find_elements(loc)
            for i in s:
                return i.text

        except Exception as e:
            print(f'获取文本失败，原因：{e}')

    # 进入frame框架
    def frame(self, loc):
        try:
            self.driver.switch_to.frame(self.find_element(loc))
        except Exception as e:
            print(f"切换到frame {loc} 失败: {e}")

    # 退出frame框架
    def content(self):
        try:
            self.driver.switch_to.default_content()
        except Exception as e:
            print(f"退出frame框架 失败: {e}")

    # 滚动条事件
    def scroll(self, x):
        try:
            self.driver.execute_script(f'window.scrollTo(0,{x})')
        except Exception as e:
            print(f'滚动失败，原因{e}')
